package uk.ac.salfordcc.sccmobile;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.List;

/**
 * Created by bwarrington on 07/08/13.
 */
public class mapTestActivity extends Activity {
    private GoogleMap mMap;
    private String[] mCentreNames, mCentreLatLng;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mDrawerTitle, mTitle;

    @Override
    protected void onResume(){
        super.onResume();
        setUpMapIfNeeded();     // Check and/or initialise the GoogleMap object
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maptest);
        setUpMapIfNeeded();     // Check and/or initialise the GoogleMap object

        mTitle = mDrawerTitle = getTitle();

        mCentreNames = getResources().getStringArray(R.array.collegeCentreNames);
        mCentreLatLng = getResources().getStringArray(R.array.collegeCentreLatLng);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item_menu_map, mCentreNames));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        /*
        //getActionBar().setDisplayHomeAsUpEnabled(true);
        //getActionBar().setHomeButtonEnabled(true);
        getActionBar().hide();

        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                R.drawable.ic_drawer,
                R.string.drawer_open,
                R.string.drawer_closed
        ){
            public void onDrawerClosed(View view){
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu();
            }
            public void onDrawerOpened(View view){
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        if(savedInstanceState == null){
            //selectItem(0);
        }
        //getActionBar().setIcon(R.drawable.ic_drawer);
        */
    }

    @Override
    public void setTitle(CharSequence title){
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mapdrawer_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
/*
    @Override
    public boolean onPerpareOptionsMenu(Menu menu){
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_toggleDrawer).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }
*/


    private class DrawerItemClickListener implements ListView.OnItemClickListener{
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id){
            selectItem(position);
        }
    }

    private void selectItem(int position){
        mDrawerLayout.closeDrawer(mDrawerList);

        String[] ltlg = mCentreLatLng[position].split(",");
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Float.parseFloat(ltlg[0]), Float.parseFloat(ltlg[1])), 15));

        //Log.i("scc Map Menu Position: ", Integer.toString(position));
        //Toast.makeText(mapTestActivity.this, mCentreLatLng[position], Toast.LENGTH_SHORT);
    }
    // =============================================================================================
    private void setUpMapIfNeeded(){
        if(mMap == null){
            mMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
            if(mMap != null){
                mMap.setMyLocationEnabled(true);
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(53.493152, -2.323635), 11));
                addMarkers();
            }
        }
    }
    // =============================================================================================
    private void addMarkers(){
        /*
        53.496082, -2.338201    Eccles
        53.495381, -2.303374    Pendleton
        53.491955, -2.277491    City
        53.476688, -2.294705    Future Skills
        53.468993, -2.283538    Frontier House
        53.516015, -2.396437    Walkden Centre
         */



        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(53.516015, -2.396437))
                .title("Walkden Centre"));
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(53.496082, -2.338201))
                .title("Eccles Centre"));
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(53.495381, -2.303374))
                .title("Pendleton Centre"));
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(53.491955, -2.277491))
                .title("City Centre"));
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(53.476688, -2.294705))
                .title("Future Skills"));
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(53.468993, -2.283538))
                .title("Frontier House"));
    }

}